package org.at.diary.user;

import org.at.nhatky1.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterUser extends Activity {

	EditText edtUsername; String txtUsername;
	EditText edtPassword; String txtPassword;
	EditText edtReTypePassword; String txtReTypePassword;
	EditText edtEmail; String txtEmail;
	EditText edtReTypeEmail; String txtReTypeEmail;
	Button btnOk;
	Button btnCancel;
	
	UserDatabaseAdapter userHelper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		userHelper = new UserDatabaseAdapter(RegisterUser.this);
		userHelper.open();
		
		edtUsername = (EditText) findViewById(R.id.edtUsername);
		edtPassword = (EditText) findViewById(R.id.edtPassword);
		edtReTypePassword = (EditText) findViewById(R.id.edtReTypePassword);
		edtEmail = (EditText) findViewById(R.id.edtEmail);
		edtReTypeEmail = (EditText) findViewById(R.id.edtReTypeEmail);
		btnOk = (Button) findViewById(R.id.btnOk);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txtUsername = edtUsername.getText().toString();
				txtPassword = edtPassword.getText().toString();
				txtReTypePassword = edtReTypePassword.getText().toString();
				txtEmail = edtEmail.getText().toString();
				txtReTypeEmail = edtReTypeEmail.getText().toString();
				
				
				if (txtUsername.equals("")|| txtPassword.equals("")|| txtReTypePassword.equals("")|| txtEmail.equals("")|| txtReTypeEmail.equals("")){
					Toast.makeText(RegisterUser.this, "Xin vui lòng nhập đầy đủ thông tin", Toast.LENGTH_SHORT).show();
				}
				else if (!txtPassword.equals(txtReTypePassword)){
					
					Toast.makeText(RegisterUser.this, "Mật khẩu bạn nhập không giống nhau", Toast.LENGTH_SHORT).show();
				}
				else if (!txtEmail.equals(txtReTypeEmail)) {
					Toast.makeText(RegisterUser.this, "Email bạn nhập không giống nhau", Toast.LENGTH_SHORT).show();
				}
				else{
					if (isExistTable() == false){
						userHelper.createUser(txtUsername, txtPassword, txtPassword);
						Toast.makeText(RegisterUser.this, "Đăng ký thành công, Xin cảm ơn!", Toast.LENGTH_LONG).show();
						
						Intent i = getBaseContext().getPackageManager()
							.getLaunchIntentForPackage( getBaseContext().getPackageName());
							i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(i);
						
					}
					else{
						Toast.makeText(RegisterUser.this, "Bạn đã đăng ký thông tin. Vui long chọn \"Hủy\" để đăng nhập", Toast.LENGTH_LONG).show();
					}
				}
			}
		});
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	
	public boolean isExistTable(){
		Cursor c = userHelper.fetchAllNotes();
        int i = c.getCount();
        if (i==0) return false;
		else return true;
		
	}
	
	
}
